package com.uottawa.clickerapp.views;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;

import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;

import com.uottawa.clickerapp.R;
import com.uottawa.clickerapp.domain.*;
import com.uottawa.clickerapp.service.ClickerAppWebServiceClient;
import com.uottawa.clickerapp.service.impl.*;

public class VoteActivity extends Activity implements OnClickListener {
	
	//private Question currentQuestion;

	private QuestionSession questionSession;
	private Question theQuestion;
	private String session;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

        Intent fromQuestion = getIntent();
	    String questionText = (String) fromQuestion.getSerializableExtra("QuestionText");
	    session = (String) fromQuestion.getSerializableExtra("session");
        new Thread(new Runnable() {
            public void run() {
            	ClickerAppWebServiceClient client =	ClickerAppWebServiceClientImpl.getInstance();
            	questionSession = client.getQuestions(session);
            }
          }).start();
        try {
			Thread.sleep(500);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        theQuestion = null;
        List<Question> questions = new ArrayList<Question>();
        questions = questionSession.getQuestions();
        for (int x = 0; x < questions.size(); x++)
        {
        	if (questions.get(x).getQuestionText().equals(questionText))
        		theQuestion = questions.get(x);
        }
        if (theQuestion != null)
        	loadVotes(theQuestion);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
//d		getMenuInflater().inflate(  , menu); FIX THIS AS WELL
		return true;
	}
	
	private void loadVotes(Question theQuestion){
		
		LinearLayout ll = new LinearLayout(this);
		ll.setOrientation(LinearLayout.VERTICAL);
		LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
		     LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
		
		ll.setGravity(Gravity.CENTER_HORIZONTAL);
		
		TextView lblQuestion = new TextView(this);
		lblQuestion.setTextSize(14);
		lblQuestion.setText(theQuestion.getQuestionText());
		ll.addView(lblQuestion, layoutParams);
		
		for(Answer a: theQuestion.getAnswers()){
			
			Button cmdAnswer = new Button(this);
			cmdAnswer.setText(a.getAnswerText());
			cmdAnswer.setTag(a);
			cmdAnswer.setGravity(Gravity.CENTER);
			ll.addView(cmdAnswer, layoutParams);
			cmdAnswer.setOnClickListener(this);
			//cmdAnswer.setPadding(0, 10, 0, 50);
			cmdAnswer.setWidth(300);
		}
		
		
		TextView blank = new TextView(this);
		blank.setTextSize(26);
		blank.setText("");
		ll.addView(blank, layoutParams);
		
		Button viewResults = new Button(this);
		viewResults.setText("View the Results");
		viewResults.setTag(theQuestion);
		viewResults.setGravity(Gravity.CENTER);
		ll.addView(viewResults, layoutParams);
		viewResults.setOnClickListener(this);
		//cmdAnswer.setPadding(0, 10, 0, 50);
		viewResults.setWidth(400);
		
		setContentView(ll); 
	}

	@Override
	public void onClick(View v) {
		try
		{
			Answer a = (Answer) v.getTag();
			//ClickerAppWebServiceClient service = ClickerAppWebServiceClientImpl.getInstance();
			//Context c = v.getContext();
			//Dialog dialog = new Dialog(c);
			//String s = "q:" + theQuestion.getId() + " a: " + a.getId();
			////.setTitle(s);
			//dialog.show();
			postPoll(a.getId());
			//service.submitPoll(theQuestion.getId(), a.getId());
		}
		catch (ClassCastException e) {
			e.printStackTrace();
			Question q = (Question) v.getTag();
			ClickerAppWebServiceClient service = ClickerAppWebServiceClientImpl.getInstance();
			Context c = v.getContext();
			//Dialog dialog = new Dialog(c);
			//String s = "q:" + q.getQuestionText();
			//dialog.setTitle(s);
			//.show();
			Intent goResults = new Intent(VoteActivity.this,PollsActivity.class);
			goResults.putExtra("QuestionText", q.getQuestionText());
			goResults.putExtra("QuestionID", q.getId());
			goResults.putExtra("session", session);
			startActivity(goResults);
			//service.submitPoll(theQuestion.getID(), a.getID());
		}
	}
	
	private void postPoll(final long aid){

		 new Thread(new Runnable() {
	            public void run() {
	            	ClickerAppWebServiceClient service = ClickerAppWebServiceClientImpl.getInstance();
	            	service.submitPoll(theQuestion.getId(), aid);
	            }
	          }).start();
	        try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}

	}
}
